﻿using System;
using System.Globalization;
using System.Linq;
using System.Web.UI;
using AB.ServiceDirectory.BO;
using AB.ServiceDirectory.Commons.DataFilters;
using AB.ServiceDirectory.Dao;
using AB.ServiceDirectory.Entities;

namespace AB.ServiceDirectory.Design.HContact
{
    public partial class AddContact02 : Page
    {
        /// <summary>
        /// Handles the Load event of the Page control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindDropDownListContactType();
                BindDropDownListBestContactMethod();

                var filter = new HContactFilter();
                ViewState["filter"] = filter;
            }
        }

        /// <summary>
        /// Binds the type of the drop down list contact.
        /// </summary>
        protected void BindDropDownListContactType()
        {
            DropDownListContactType.DataSource = HungContactBO.SelectAllContactTypes();
            DropDownListContactType.DataBind();
        }

        /// <summary>
        /// Binds the drop down list best contact method.
        /// </summary>
        protected void BindDropDownListBestContactMethod()
        {
            DropDownListBestContactMethod.DataSource = HungContactBO.SelectAllBestMethodContact();
            DropDownListBestContactMethod.DataBind();
        }

        /// <summary>
        /// Gets the contact from fields in page
        /// </summary>
        /// <returns></returns>
        protected Contact LoadContact()
        {
            int contactTypeId = int.Parse(DropDownListContactType.SelectedValue,
                                          CultureInfo.CurrentCulture);
            int bestContactMethodId = int.Parse(DropDownListBestContactMethod.SelectedValue, CultureInfo.CurrentCulture);
            ContactType contactType = DB.Context.ContactTypes.FirstOrDefault(c => c.ContactTypeId == contactTypeId);
            BestContactMethod bestContactMethod =
                DB.Context.BestContactMethods.FirstOrDefault(c => c.BestContactMethodId == bestContactMethodId);
            var contact = new Contact
                              {
                                  FirstName = TextBoxFirstName.Text,
                                  Surname = TextBoxSurname.Text,
                                  KnownAs = TextBoxKnowAs.Text,
                                  OfficePhone = TextBoxOfficePhone.Text,
                                  MobilePhone = TextBoxMobilePhone.Text,
                                  HomePhone = TextBoxHomePhone.Text,
                                  Email = TextBoxEmailAddress.Text,
                                  ManageId =
                                      string.IsNullOrEmpty(HiddenFieldManagerId.Value)
                                          ? (int?)null
                                          : Convert.ToInt32(HiddenFieldManagerId.Value, CultureInfo.CurrentCulture),
                                  ContactType = contactType,
                                  BestContactMethod = bestContactMethod,
                                  JobRole = TextBoxRole.Text,
                                  WorkBase = TextBoxWorkBase.Text,
                                  JobTitle = TextBoxJobTitle.Text,
                                  IsActive = CheckBoxIsActive.Checked
                              };

            return contact;
        }

        /// <summary>
        /// Handles the Click event of the ButtonSave control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void ButtonSaveClick(object sender, EventArgs e)
        {
            Contact contact = LoadContact();
            bool result = HungContactBO.InsertContact(contact);
            if (result)
            {
                Response.Redirect("~/Contact_HungND12/ContactList.aspx");
            }
        }

        /// <summary>
        /// Handles the Click event of the LinkButtonLookup control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void LinkButtonLookupClick(object sender, EventArgs e)
        {
            ContactLookup.Popup();
        }

        /// <summary>
        /// Handles the Click event of the ButtonBack control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void ButtonBackClick(object sender, EventArgs e)
        {
            Response.Redirect("ContactList.aspx");
        }

        #region Popupwindow

        #endregion Popupwindow
    }
}